package by.qizer.supercamp.web;

import by.qizer.supercamp.domain.Inside;
import by.qizer.supercamp.service.InsideService;
import by.qizer.supercamp.service.JournalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Map;

/**
 * @author Pribytkoyski Alexey
 *         User: qizer
 *         Date: 08.04.12
 */
@Controller
public class InsideController {

    @Autowired
    private InsideService insideService;

    @Autowired
    private JournalService journalService;

    @RequestMapping(value = "/insideadd", method = RequestMethod.POST)
    public String addInside(@ModelAttribute("inside") Inside inside, BindingResult result) {
        insideService.addInside(inside);
        return "redirect:/inside";
    }

    @RequestMapping("/inside")
    public String listInside(Map<String, Object> map) {
        map.put("inside", new Inside());
        map.put("insideList", insideService.listInside());
        return "inside";
    }

    @RequestMapping("/insidedel/{insideId}")
    public String deleteInside(@PathVariable("insideId") Integer insideId) {

        System.out.println("\n\n\t send delete");

        Inside inside = insideService.removeInside(insideId);

        System.out.println("\n\n\t delete successfully");

        System.out.println("\n\t" + inside.getAutoId().toString() + "\n");
        System.out.println("\n\t" + inside.getGuardId().toString() + "\n");
        System.out.println("\n\t" + inside.getInTime().toString() + "\n");

        journalService.addJournal(inside);
        return "redirect:/inside";
    }
}
